package cn.lin.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author cn.Lin
 * @create 2024-12-04-16:55
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
        // 能够获取连接
        redisTemplate.setConnectionFactory(factory);
        // 设置Key以及Value的序列化方式
        // key--->String
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        // 由spring-data-redis进行提供的打包JSON
        // value--->JSON
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        return redisTemplate;
    }
}
